/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dominio.Controladores;

import Dominio.IdFrec;
import Dominio.NoticiaNoValidaException;
import Dominio.ParserDisc;
import Dominio.ParserHttp;
import Dominio.Strouble;
import Dominio.Tema;
import Dominio.Texto;
import Dominio.TitTex;
import Dominio.VecTitTex;
import Vistas.FrecPal;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Vector;
import vistas2.AuxNoticia;

/**
 *
 * @author sandra
 */
public class CtrlClasificacion {

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StringBuffer str = new StringBuffer();
    CtrlDominio CD = CtrlDominio.getInstance();

    public CtrlClasificacion() {
    }

    public VecTitTex clasificar(String pa) throws NoticiaNoValidaException, NullPointerException, MalformedURLException, IOException {
        Vector<String> vec = new Vector<String>(0, 1);
        TitTex tt = new TitTex();
        Texto tx;
        VecTitTex vtt = new VecTitTex();

        if (pa.substring(0, 4).compareTo("http") == 0) {
            URL page = new URL(pa);
            HttpURLConnection conn = (HttpURLConnection) page.openConnection();
            conn.connect();
            InputStreamReader inr = new InputStreamReader((InputStream) conn.getContent());
            BufferedReader buff = new BufferedReader(inr);
            String line;
            do {
                line = buff.readLine();
                str.append(line + " ");
            } while (line != null);
            ParserHttp not = new ParserHttp();
            tt = not.sacaTexto(str);

        } else {
            File fil = new File(pa);
            BufferedReader di = new BufferedReader(new FileReader(fil));
            String linea;
            do {
                linea = di.readLine();
                if (linea == null) {
                    break;
                } else {
                    str.append(linea + " ");
                }
            } while (true);
            ParserDisc not = new ParserDisc();
            tt = not.sacaTexto(str);
        //System.out.println(tx.consultarTexto());
        //System.out.println(tx.consultarBiClaves());
        //System.out.println(tx.consultarClaves());
        }


        //try??
        str = new StringBuffer("");
        tx = new Texto(tt.texto, CD.getDiccionarioPalabrasNoFuncionales());//System.out.println("que hay en texto: "+tt.texto);
        vtt.vec = new Vector<FrecPal>();
        vtt.vec = tx.consultarClaves();
        vtt.tit = tt.titulo;
        vtt.texto = tt.texto;
        return vtt;
    }

    public Vector<Strouble> sacaTemas(VecTitTex vtt) {
        FrecPal fp;
        Vector<Strouble> vs = new Vector<Strouble>(0, 1);
        Vector<IdFrec> vif = new Vector<IdFrec>(0, 1);
        int id;
        for (int i = 0; i < vtt.vec.size(); i++) {
            fp = (FrecPal) vtt.vec.get(i);
            if (CD.existLetra(fp.pal.charAt(0))) {
                id = CD.existPalabra(fp.pal);
                if (id != 0) {
                    if (CD.palabraActivada(fp.pal)) {
                        IdFrec idf = new IdFrec();
                        idf.id = id;
                        idf.frec = fp.frec;
                        vif.add(idf);
                    }
                }
            }
        }

        Vector<Tema> vt = CD.getTemas();
        for (Tema t : vt) {
            Strouble st = new Strouble();
            st.dist = t.getDistancia(vif, CD.sizeNoticiasManual(), CD.getDiccioanrioPalabras());
            System.out.println(st.dist);
            st.nom = t.getNombre();
            if (st.dist > 0) {
                vs.add(st);
            }
        }
        Collections.sort(vs);
        return vs;
    }
    
    public boolean estaNoticiaDiccionario(String not){
        return CD.estaNoticia(not);
    }
    
    public void guardarClasificacionManual(AuxNoticia aux){
        CD.guardarClasificacionManual(aux);
    }
    
    public boolean estaDiccionarioNoticias(String not) {
        return CD.estaNoticia(not);
    }
}
